package com.slmxz.tms.document.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.slmxz.tms.common.enums.BizMessageEnum;
import com.slmxz.tms.common.model.Option;
import com.slmxz.tms.core.exception.Asserts;
import com.slmxz.tms.core.exception.BizException;
import com.slmxz.tms.common.result.ResultCode;
import com.slmxz.tms.document.mapper.CustomerMapper;
import com.slmxz.tms.document.model.entity.Customer;
import com.slmxz.tms.document.model.vo.CustomerVO;
import com.slmxz.tms.document.service.CustomerService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;
import java.util.Arrays;
import java.util.List;
import java.util.ArrayList;
import cn.hutool.core.util.StrUtil;

/**
 * 客户服务实现类
 * @author sunyao
 * @Date 2024-11-08
 */
@Service
@RequiredArgsConstructor
public class CustomerServiceImpl extends ServiceImpl<CustomerMapper, Customer> implements CustomerService {

    /**
    * 获取客户分页列表
    * @return {@link IPage< CustomerVO >} 客户分页列表
    */
    @Override
    public IPage<CustomerVO> selectCustomerPage(IPage<CustomerVO> page,CustomerVO customer) {
        return page.setRecords(this.baseMapper.selectCustomerPage(page,customer));
    }

    @Override
    public List<Option<String>> selectOption(CustomerVO customer) {
        return this.baseMapper.selectOption(customer);
    }

    /**
     * @param id 客户ID
     * @return
     */
    @Override
    public CustomerVO selectCustomerDetail(Long id) {
        Customer entity = this.getById(id);
        CustomerVO customer = BeanUtil.copyProperties(entity,CustomerVO.class);
        return customer;
    }
    
    /**
     * 新增客户
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean saveCustomer(CustomerVO customer) {
        return this.save(customer);
    }
    
    /**
     * 更新客户
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateCustomer(CustomerVO customer) {
        return this.updateById(customer);
    }
    
    /**
     * 删除客户
     *
     * @param ids 客户ID，多个以英文逗号(,)分割
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteCustomers(String ids) {
        Asserts.fail(StrUtil.isBlank(ids),BizMessageEnum.PARAM_IS_NULL);
        // 逻辑删除
        List<Long> idList = Arrays.stream(ids.split(","))
                .map(Long::parseLong)
                .toList();
        return this.removeByIds(idList);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean activeOrLogout(CustomerVO customer) {
        //激活或者注销业务数据
        Asserts.fail((CollectionUtil.isEmpty(customer.getIds()) || customer.getIsDeleted() == null),BizMessageEnum.PARAM_IS_NULL);

        List<Customer>  customerList = new ArrayList<>(customer.getIds().size());
        customer.getIds().forEach(id -> {
            Customer customerParam = new Customer();
            customerParam.setId(id);
            customerParam.setIsDeleted(customer.getIsDeleted());
            customerList.add(customerParam);
        });
        this.updateBatchById(customerList,10);
        return true;
    }

}
